package cn.xc;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.Test;

import java.util.HashMap;

public class EsTest {

    @Test
    public void testAdd() throws Exception{
        //ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();

        // 创建要新增的数据
        for (int i = 0; i < 98; i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", i);
            map.put("name", "柯基" + i);
            map.put("age", i);
            map.put("sex", i%2);
            IndexResponse response = client.prepareIndex("pethome", "pet", i+"").setSource(map).get();
            System.out.println(response);
        }

        client.close();
    }

    @Test
    public void testGet() throws Exception{
        //获取ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();
        // 指定索引得到查询对象
        GetRequestBuilder builder = client.prepareGet("pethome", "pet", "1");
        System.out.println(builder.get().getSource());
    }


    @Test
    public void testUpdate() throws Exception{
        // 获取ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();
        // 指定索引得到查询对象
        UpdateRequestBuilder builder = client.prepareUpdate("pethome", "pet", "1");
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", "金毛");
        UpdateResponse updateResponse = builder.setDoc(map).get();
        System.out.println(updateResponse);
    }


    @Test
    public void testDelete() throws Exception{
        // 获取ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();
        // 指定索引、类型、Id得到查询对象
        DeleteRequestBuilder builder = client.prepareDelete("pethome", "pet", "1");

        System.out.println(builder.get());
    }

    @Test
    public void test1() throws Exception{
        // 获取ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();
        // 设置要条件查询的索引
        SearchRequestBuilder builder = client.prepareSearch("pethome");
        // 设置搜索条件
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

        boolQuery.must(QueryBuilders.matchQuery("name", "发单色光"));

        boolQuery.filter(QueryBuilders.termQuery("sex", 1))
                .filter(QueryBuilders.rangeQuery("age").gte(1).lte(12));

        builder.setQuery(boolQuery);
        // 设置分页和排序
        builder.setFrom(2);
        builder.setSize(2);
        builder.addSort("age", SortOrder.DESC);
        SearchResponse searchResponse = builder.get();
        SearchHit[] hits = searchResponse.getHits().getHits();
        for (SearchHit hit : hits) {
            System.out.println(hit.getSourceAsMap());
        }
    }


    @Test
    public void testBatch() throws Exception {
        // 获取ES连接
        TransportClient client = cn.xc.ESClientUtil.getClient();
        // 创建批量操作对象
        BulkRequestBuilder bulk = client.prepareBulk();
        // 批量创建文档，放入批量操作对象中
        for (int i = 2; i < 100; i++) {
            IndexRequestBuilder builder = client.prepareIndex("pethome", "user", i + "").setSource(
                    XContentFactory.jsonBuilder()
                            .startObject()
                            .field("id", i - 1)
                            .field("name", "阿尔用户和!" + i)
                            .field("age", i - 1)
                            .field("sex", i%2)
                            .endObject()
            );
            BulkResponse result = bulk.add(builder).get();
            System.out.println(result.status());
        }
        // 关闭连接
        client.close();
    }

}
